# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
触发中移动项目的对接上海银行接口
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

import os
import time

from msydwork.api.api_batch import exec_fileLog,exec_batch
from msydwork.api.api_msydloan import apply, freeze, block
from msydwork.api.api_msydloan_web import create_loanFile,upload_loanFile
from msydwork.data.mysql_msydloan import get_file, get_org,update_msydloan
from msydwork.config.config_msydloan import ip,sftp_user,sftp_password,c_url
from pycacho.cachobase.deal_time import minus_date
from pycacho.cachobase.get_base_info import process_msydloan
from pycacho.cachobase.file_rename import copy_rename_file
from pythonlession.recordmodule.paramiko_model import SftpDeal

headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
            'content-Type': 'application/json',
        }

sftp = SftpDeal(ip,sftp_user,sftp_password)

# 批量发起授信，带图片
def batch_apply(n,m):
    sample = 'E:\zenglingwei\\test\pic\\test.jpg'
    new_path = 'E:\zenglingwei\\test\\batch\\'
    remote_base = '/data/sftp/hb/home/upload/hb/send/credit/2020/04/'
    info = process_msydloan(200)
    for i in range(n,m):
        id_card = info[i][0]
        name = info[i][1]
        mobile = info[i][3]
        hb_no = 'HB' + id_card
        front_id_name = minus_date(0, 's') + '%04d' % (i) + '-1' + os.path.basename(sample)
        front_file = copy_rename_file(sample, front_id_name,new_path)            # 复制一个图片
        sftp.upload(front_file,os.path.join(remote_base, front_id_name))  # 上传到sftp

        back_id_name = minus_date(0, 's') + '%04d' % (i) + '-2' + os.path.basename(sample)
        back_file = copy_rename_file(sample, back_id_name,new_path)
        sftp.upload(back_file,os.path.join(remote_base, back_id_name))
        apply(name, mobile, id_card, hb_no, front_id_name, back_id_name,'05')

# 授信、借款、办理套餐成功通知  第n个到m-1个
def flow(n,m):
    sample = 'E:\zenglingwei\\test\pic\\test.jpg'
    new_path = 'E:\zenglingwei\\test\\batch\\'
    remote_base = '/data/sftp/hb/home/upload/hb/send/credit/2020/12/'
    info = process_msydloan(200)
    prov = '18'
    for i in range(n, m):
        id_card = info[i][0]
        name = info[i][1]
        mobile = info[i][3]
        hb_no = 'HB' + id_card
        front_id_name = minus_date(0, 's') + '%04d' % (i) + '-1' + os.path.basename(sample)
        front_file = copy_rename_file(sample, new_path,front_id_name)  # 复制一个图片
        sftp.upload(front_file, os.path.join(remote_base, front_id_name))  # 上传到sftp

        back_id_name = minus_date(0, 's') + '%04d' % (i) + '-2' + os.path.basename(sample)
        back_file = copy_rename_file(sample, new_path,back_id_name)
        sftp.upload(back_file, os.path.join(remote_base, back_id_name))
        apply(name, mobile, id_card, hb_no, front_id_name, back_id_name,prov)

        time.sleep(2)          # 借款
        brwOrdNo = info[i][2]
        freeze(hb_no,brwOrdNo,mobile,id_card,prov,'24','20')

        time.sleep(2)         # 套餐办理成功通知
        orgOrdNo = get_org(brwOrdNo)
        brwOrdDt = minus_date(3,'s')
        acpTm = minus_date(2,'q')
        time.sleep(2)
        block(brwOrdNo,orgOrdNo,mobile,brwOrdDt,acpTm)

# 生成分省办理成功订单DAT文件、删除服务器旧的xls文件、web端生成xls文件、下载xls文件到本地、本地xls放款文件导入放款
def deal_file():
    update_msydloan(minus_date(1, 't'))  # 更新当天日期
    #exec_batch(c_url)  # 跑当天批次
    time.sleep(1)
    folder = '/data/sftp/hb/home/upload/hb/send/'
    date = minus_date(1,'s') # '20200919'
    provs = ['05', '18', '21','23','28']
    for p in provs:
        # 1. 生成dat文件
        file = get_file('01', p, date)  # 初始生成
        #file = 'F:\\HBCBF_' +p +'_888190900238401_01_' + date +'_001.DAT'
        remote_dat = folder + date + '/' + os.path.basename(file)
        sftp.upload(file, remote_dat)  # 2. 上传dat文件
    # 处理xls文件
    xls_file = 'F:/' + date + 'loanFile.xls'  #  '20201214loanFile.xls'  date + 'loanFile.xls'
    remote_xls = folder + date + '/' + os.path.basename(xls_file)
    time.sleep(2)                # 3. 删除旧的xls文件
    #sftp.remove(remote_xls)
    time.sleep(2)                # 4. web端生成xls文件
    #create_loanFile(date)
    time.sleep(2)                # 5. 下载xls文件到本地
    #sftp.download(remote_xls, xls_file)
    time.sleep(2)               # 6. 本地xls放款文件导入
    upload_loanFile(xls_file, date)

# 批量扣款回盘文件处理
def call_back():
    #exec_batch()
    #time.sleep(10)
    folder = '/data/sftp/hb/home/upload/hb/send/'
    date = '20200910'                      # 回盘文件日期
    file = 'F://BACK_MPLCASH_888190900238401_DEDUCTION_20200910_001.DAT.old'       # 初始生成批量扣款回盘文件(成功放款)      get_file('99', '18', date)
    remote_dat = folder + date + '/' +  os.path.basename(file)
    sftp.upload(file, remote_dat)          # 上传文件到sftp
    exec_fileLog(c_url,'msydloan',date)                         # 生成文件定时任务记录表，最后去xx-cronjob执行脚本


def temp_upload():
    file_name = 'LX_PAY_MCH202005150003_20200726.csv'
    #sftp.remove('/data/sftp/zydjsb/jsb/encourage/result/'+ file_name)
    sftp.upload('F:/' + file_name , '/data/sftp/zydjsb/jsb/encourage/result/'+ file_name )

if __name__ == '__main__':
    q = 10
    if q ==100:
        #truncate_table()
        flow(1, 2)
    elif q == 10:
        #flow(1, 2)
        deal_file()
    elif q == 11:
        batch_apply(26,27)
    elif q == 12:
        temp_upload()
    else:
        call_back()